from web.forms.boot_strap import BootStrap
from django import forms
from web import models


class IssuesModelForm(BootStrap, forms.ModelForm):

    class Meta:
        model = models.Issues
        exclude = ["project", "creator", "create_datetime", "update_datetime"]
        widgets = {
            "assign": forms.Select(attrs={"class": "selectpicker", "data-live-search": "true"}),
            "attention": forms.SelectMultiple(attrs={"class": "selectpicker",
                                                     "data-live-search": "true",
                                                     "data-actions-box": "true"}),
            "parent": forms.Select(attrs={"class": "selectpicker", "data-live-search": "true"}),
        }

    def __init__(self, request,  *args, **kwargs):
        super().__init__(*args, **kwargs)

        # 1.为IssueType字段添加choices
        issue_type_list = models.IssueType.objects.filter(project=request.tracer.project).values_list("id", "title")
        self.fields["issues_type"].choices = issue_type_list
        # 2. 为Module字段添加choices
        module_list = [("", "没有选中任何选项")]
        module_object_list = models.Module.objects.filter(project=request.tracer.project).values_list("id", "title")
        module_list.extend(module_object_list)
        self.fields["module"].choices = module_list

        # 3. 对assign和attention字段进行自定义choices
        # 首先找到当前项目的创建者 和 参与者
        user_list = [(request.tracer.project.creator.id, request.tracer.project.creator.username)]
        user_object_list = models.ProjectUser.objects.filter(project=request.tracer.project).values_list("id", "user")
        user_list.extend(user_object_list)
        self.fields["assign"].choices = [("", "没有选中任何选项")] + user_list
        self.fields["attention"].choices = user_list

        # 4. 对parent字段自定义choices
        parent_list = [("", "没有选中任何选项")]
        parent_object_list = models.Issues.objects.filter(project=request.tracer.project).values_list("id", "subject")
        parent_list.extend(parent_object_list)
        self.fields["parent"].choices = parent_list


class IssuesReplyModelForm(forms.ModelForm):
    class Meta:
        model = models.IssuesReply
        fields = ["content", "reply"]


class InviteModelForm(BootStrap, forms.ModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ["period", "count"]



